package com.ehome.poct.terminal.controller;

import cn.hutool.json.JSONUtil;
import com.ehome.core.page.PageInfo;
import com.ehome.poct.core.api.dto.check.AlgoNotifyDTO;
import com.ehome.poct.core.api.dto.check.CheckReqDTO;
import com.ehome.poct.core.api.dto.check.CheckStatusDTO;
import com.ehome.poct.core.api.dto.check.TestItemVO;
import com.ehome.poct.core.api.dto.order.TestOrderCheckDTO;
import com.ehome.poct.core.api.vo.check.CellBoardValidVO;
import com.ehome.poct.core.api.vo.check.CheckProcessResultVO;
import com.ehome.poct.core.api.vo.check.CheckStatusVO;
import com.ehome.poct.core.api.vo.order.TestOrderCheckRecordVO;
import com.ehome.poct.terminal.api.fegin.TestOrderCheckRecordClient;
import com.ehome.poct.terminal.api.qry.CheckRecordQry;
import com.ehome.poct.terminal.service.CheckService;
import com.ehome.web.annotation.RequestLog;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Optional;

/**
 * 患者
 *
 * @author cq
 * @since 2022/03/02 11:21
 **/
@Api(tags = "检测流程")
@ApiSupport(author = "cq")
@RequestMapping("/check")
@RestController
@AllArgsConstructor
@Slf4j
public class CheckController {

    @Autowired
    private CheckService checkServ;
    @Autowired
    private TestOrderCheckRecordClient checkRecordClient;

    @ApiOperation("开始检测处理")
    @ApiOperationSupport(author = "cq")
    @PostMapping("/startCheck")
    @RequestLog
    public CheckProcessResultVO doCheckProcess(@Valid @RequestBody CheckReqDTO checkParam) {
        log.info("startCheck request [{}]", JSONUtil.toJsonStr(checkParam));
        return checkServ.doCheckProcess(checkParam);
    }

    @ApiOperation("查询检测状态（轮询）")
    @ApiOperationSupport(author = "cq")
    @GetMapping("/poll/status/{id}")
    @RequestLog
    public CheckStatusVO pollGetStatus(@PathVariable @ApiParam(value = "检测数据主键ID", required = true) Long id) {
        return checkServ.pollGetStatus(id);
    }

    @ApiOperation("查询检测状态（手动）")
    @ApiOperationSupport(author = "cq")
    @GetMapping("/manual/status/{id}")
    @RequestLog
    public CheckStatusVO manualGetStatus(@PathVariable @ApiParam(value = "检测数据主键ID", required = true) Long id) {
        return checkServ.manualGetStatus(id);
    }

    @ApiOperation("算法分析完成通知")
    @ApiOperationSupport(author = "wyc")
    @PostMapping("/algo/callBack")
    @RequestLog
    public void algoCallBack(@Valid @RequestBody AlgoNotifyDTO param) {
        checkServ.algoNotify(param);
    }

    @ApiOperation("校验计数板是否存在")
    @ApiOperationSupport(author = "wyc")
    @GetMapping("/exist/{sno}")
    @RequestLog
    public Boolean boardIsExist(@PathVariable String sno) {
        return checkServ.boardIsExist(sno);
    }

    @ApiOperation("校验计数板是否存在，并返回板子厚度信息")
    @ApiOperationSupport(author = "wyc")
    @GetMapping("/thickness/{sno}")
    @RequestLog
    public CellBoardValidVO boardThickness(@PathVariable String sno) {
        return checkServ.boardThickness(sno);
    }

    @ApiOperation("待检测列表")
    @ApiOperationSupport(author = "muqiu")
    @GetMapping("/getCheckNum")
    @RequestLog
    public PageInfo<TestOrderCheckRecordVO> getCheckNum(@SpringQueryMap TestOrderCheckDTO testOrderCheckDTO) {
        return checkServ.getCheckNum(testOrderCheckDTO);
    }

    @ApiOperation("待检测项")
    @ApiOperationSupport(author = "muqiu")
    @GetMapping("/testItems/{orderNo}")
    @RequestLog
    public List<TestItemVO> testItems(@PathVariable String orderNo) {
        return checkServ.testItems(orderNo);
    }

    @ApiOperation("异常检测上报")
    @ApiOperationSupport(author = "wyc")
    @PutMapping("/abnormal/report/{orderNo}")
    public void abnormalReport(@PathVariable String orderNo) {
        checkRecordClient.updateRemark(orderNo, 1);
    }

    @ApiOperation("查询设备的联机检测状态")
    @ApiOperationSupport(author = "wyc")
    @GetMapping("/status/{deviceType}/{orderNo}")
    @RequestLog
    public Boolean abnormalReport(@PathVariable @ApiParam("设备类型 1：金标仪；2：血常规") Integer deviceType, @PathVariable String orderNo) {
        final var record = checkRecordClient.queryOne(new CheckRecordQry().setTestType(deviceType).setOrderNo(orderNo));
        return Optional.ofNullable(record).map(it -> it.getStatus().equals(1)).orElse(Boolean.FALSE);
    }


    @ApiOperation("修改检测状态")
    @ApiOperationSupport(author = "muqiu")
    @PostMapping("/updateCheckStatus")
    @RequestLog
    public void updateCheckStatus(@RequestBody CheckStatusDTO checkStatusDTO) {
        checkServ.updateCheckStatus(checkStatusDTO);
    }

}
